package com.mobi.Survey.lib.activities;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.mobi.Survey.lib.R;
import com.mobi.Survey.lib.db.AlarmTable;
import com.mobi.Survey.lib.exceptions.AuthenticationException;
import com.mobi.Survey.lib.model.AlarmPojo;
import com.mobi.Survey.lib.model.AlarmRepeatPojo;
import com.mobi.Survey.lib.model.Credentials;
import com.mobi.Survey.lib.model.SurveyPojo;
import com.mobi.Survey.lib.model.SurveysJsonParser;
import com.mobi.Survey.lib.utils.Helper;

/**
 * Download surveys in background after downloading survey we will delete old
 * surveys and insert new records
 * 
 * @author Yahya Arshad
 * 
 */
public class SurveyDownloadTask extends AsyncTask<String, Integer, Integer> {

	private SurveyDownloadCallback callback;

	public static final int LOGIN_ERROR = 0;
	public static final int SWITCH_SCREEN = 1;
	public static final int NO_INTERNET_CONNECTION = 2;
	public static final int CONNECT_ERROR = 3;
	public static final int DEFAULT = 4;
	public static final int NO_SURVEY_FOUND = 5;
	protected Credentials credentials;

	protected ProgressDialog progressDialog = null;
	protected Context ctx;

	public SurveyDownloadTask(Context ctx, Credentials credentials) {
		this.ctx = ctx;
		this.credentials = credentials;
	}

	public SurveyDownloadCallback getCallback() {
		return callback;
	}

	public void setCallback(SurveyDownloadCallback callback) {
		this.callback = callback;
	}

	protected Integer doInBackground(String... params) {
		if (isCancelled()) {
			return DEFAULT;
		}
		if (Helper.isNetworkAvailable(ctx.getApplicationContext())) {

			try {
				String surveyHeaderUrl = ctx.getString(R.string.survey_list_headers_url,
						URLEncoder.encode(credentials.getUserId()), credentials.getPassword());

				InputStream loginResponse = Helper.performGetOperationI(surveyHeaderUrl);
				List<SurveyPojo> surveys = SurveysJsonParser.getInstance().isUser(loginResponse);

				publishProgress(30);

				if (surveys.size() > 0) {
					SurveyPojo.deleteAll(ctx.getApplicationContext());
					publishProgress(40);
					SurveyPojo.commitAll(ctx.getApplicationContext(), surveys);


			 
					callback.inBackground();

					return SWITCH_SCREEN;
				} else {
					return NO_SURVEY_FOUND;
				}

			} catch (AuthenticationException ex) {
				return LOGIN_ERROR;
			} catch (IOException e) {
				return CONNECT_ERROR;
			} catch (Exception e) {
				e.printStackTrace();
				return LOGIN_ERROR;
			}

		} else {
			return NO_INTERNET_CONNECTION;
		}

	}

	@Override
	protected void onProgressUpdate(Integer... values) {

		super.onProgressUpdate(values);

		if (values[0] == 30) {
			progressDialog.setMessage(Messages.DOWNLOADING_SURVEYS);
		}
		if (values[0] == 40) {
			progressDialog.setMessage(Messages.UPDATING_DATABASE);
		}
	}

	protected void onPostExecute(Integer result) {

		super.onPostExecute(result);

		callback.onPostExecute(result);
		progressDialog.dismiss();
		progressDialog = null;

	}

	protected void onPreExecute() {

		super.onPreExecute();
		AbstractScreen.saveCredentials(credentials.getUserId(), credentials.getPassword(), ctx);

		progressDialog = ProgressDialog.show(ctx, "", Messages.LOGGIN_IN);

	}

	public interface SurveyDownloadCallback {
		public void inBackground();

		public void onPostExecute(int result);
	}

}
